package com.zzl.study.auth.security.authentication.client;

import lombok.Data;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;

/**
 * @author: zhangzl
 * @date: 2024/6/11 15:52
 * @version: 1.0
 * @description: OAuth2认证客户端
 */
@Data
public class GatewayOAuth2AuthorizedClient {

    private String clientRegistrationId;

    private String principalName;

    private GatewayOAuth2AccessToken accessToken;

    private GatewayOAuth2RefreshToken refreshToken;

    public static GatewayOAuth2AuthorizedClient of(OAuth2AuthorizedClient oAuth2AuthorizedClient) {
        if (oAuth2AuthorizedClient == null) {
            return null;
        }
        GatewayOAuth2AuthorizedClient gatewayOAuth2AuthorizedClient = new GatewayOAuth2AuthorizedClient();
        gatewayOAuth2AuthorizedClient.setClientRegistrationId(oAuth2AuthorizedClient.getClientRegistration() == null ? null : oAuth2AuthorizedClient.getClientRegistration().getRegistrationId());
        gatewayOAuth2AuthorizedClient.setPrincipalName(oAuth2AuthorizedClient.getPrincipalName());
        gatewayOAuth2AuthorizedClient.setAccessToken(GatewayOAuth2AccessToken.of(oAuth2AuthorizedClient.getAccessToken()));
        gatewayOAuth2AuthorizedClient.setRefreshToken(GatewayOAuth2RefreshToken.of(oAuth2AuthorizedClient.getRefreshToken()));
        return gatewayOAuth2AuthorizedClient;
    }

}
